// 计算中间部分数据
export const midData = (data, value) => {
  let chartsData = data
  let chartsValue = value
  // 全部样本平均数、全部 图表2数据 平均数、全部样本总和、全部 图表2数据 总和
  let iAVGx = 0, iAVGr = 0, sumX = 0, sumR = 0
  let number1 = 0, number2 = 0 // 样本数量、图表2数据数量
  for (let i in chartsValue.m_arrGrpSample) {
    for (let j of chartsValue.m_arrGrpSample[i]) {
      if (j !== 0) {
        sumX += j
        number1++
      }
    }
  }
  if (number1 !== 0) {
    iAVGx = (sumX / number1)
  }
  if (chartsData.chartType !== '单值-移动极差图') {
    for (let i of chartsValue.m_arrRChart) {
      if (i !== 0) {
        sumR += i
        number2++
      }
    }
  } else {
    chartsValue.m_arrRChart[0] = 0
    for (let i of chartsValue.m_arrRChart) {
      if (i !== 0) {
        sumR += i.toFixed(3) * 1
      }
      number2++
    }
  }
  if (number2 !== 0) {
    iAVGr = (sumR / number2)
  }
  switch (chartsData.chartType) {
    case '均值-极差图': {
      chartsValue.iUCLx = (iAVGx + chartsValue.a2 * iAVGr).toFixed(3) * 1
      chartsValue.iCLx = (iAVGx).toFixed(3) * 1
      chartsValue.iLCLx = (iAVGx - chartsValue.a2 * iAVGr).toFixed(3) * 1

      chartsValue.iUCLr = (chartsValue.d4 * iAVGr).toFixed(3) * 1
      chartsValue.iCLr = (iAVGr).toFixed(3) * 1
      chartsValue.iLCLr = (chartsValue.d3 * iAVGr).toFixed(3) * 1

      chartsValue.iSigmaA = (iAVGr / chartsValue.d_2).toFixed(3) * 1
      break;
    }
    case '均值-标准差图': {
      chartsValue.iUCLx = (iAVGx + chartsValue.a3 * iAVGr).toFixed(3) * 1
      chartsValue.iCLx = (iAVGx).toFixed(2) * 1
      chartsValue.iLCLx = (iAVGx - chartsValue.a3 * iAVGr).toFixed(3) * 1

      chartsValue.iUCLr = (chartsValue.b4 * iAVGr).toFixed(3) * 1
      chartsValue.iCLr = (iAVGr).toFixed(3) * 1
      chartsValue.iLCLr = (chartsValue.b3 * iAVGr).toFixed(3) * 1

      chartsValue.iSigmaA = (iAVGr / chartsValue.c4).toFixed(3) * 1
      break;
    }
    case '中位数-极差图': {
      chartsValue.iUCLx = (iAVGx + chartsValue.a2 * iAVGr).toFixed(3) * 1
      chartsValue.iCLx = (iAVGx).toFixed(3) * 1
      chartsValue.iLCLx = (iAVGx - chartsValue.a2 * iAVGr).toFixed(3) * 1

      chartsValue.iUCLr = (chartsValue.d4 * iAVGr).toFixed(3) * 1
      chartsValue.iCLr = (iAVGr).toFixed(3) * 1
      chartsValue.iLCLr = (chartsValue.d3 * iAVGr).toFixed(3) * 1

      chartsValue.iSigmaA = (iAVGr / chartsValue.d_2).toFixed(3) * 1
      break;
    }
    case '单值-移动极差图': {
      chartsValue.iUCLx = (iAVGx + chartsValue.e2 * iAVGr).toFixed(3) * 1
      chartsValue.iCLx = (iAVGx).toFixed(3) * 1
      chartsValue.iLCLx = (iAVGx - chartsValue.e2 * iAVGr).toFixed(3) * 1

      chartsValue.iUCLr = (chartsValue.d4 * iAVGr).toFixed(3) * 1
      chartsValue.iCLr = (iAVGr).toFixed(3) * 1
      chartsValue.iLCLr = (chartsValue.d3 * iAVGr).toFixed(3) * 1

      chartsValue.iSigmaA = (iAVGr / chartsValue.d_2).toFixed(3) * 1
      break;
    }
  }

  let iCPU = 0;
  let iCPL = 0;
  let iPPU = 0;
  let iPPL = 0;
  let iSagmax = 0;
  let arr = [] // 全部样本
  let length = 0
  for (let i of chartsValue.m_arrGrpSample) {
    for (let j of i) {
      if (j !== 0) {
        arr.push(j)
        length++
      }
    }
  }

  // 和
  function sum(x, y) {
    return x + y
  }

  // 平方
  function square(x) {
    return x * x
  }

  function stddev(data, length) {
    let mean = data.reduce(sum) / (length - 1) // 平均数
    let deviations = data.map(function (x) { return x - mean })
    let stddev = Math.sqrt(deviations.map(square).reduce(sum) / (length - 2))
    return stddev
  }

  iSagmax = stddev(arr, length + 1)

  // 判断结果是否为NAN
  if (Object.is(iSagmax, NaN)) {
    iSagmax = 0
  }
  chartsValue.iSigmaS = ((chartsValue.iUSL - chartsValue.iLSL) / (chartsValue.iSigmaStd * 2)).toFixed(3) * 1
  chartsValue.iSigmaP = iSagmax.toFixed(3) * 1

  if (chartsValue.iUSL - chartsValue.iLSL !== 0) {
    chartsValue.iCa = (Math.abs(((chartsValue.iUSL + chartsValue.iLSL) / 2 - iAVGx) / ((chartsValue.iUSL - chartsValue.iLSL) / 2))).toFixed(3) * 1
  }
  if (iAVGr != 0) {
    chartsValue.iCp = ((chartsValue.iUSL - chartsValue.iLSL) / (6 * (chartsValue.iSigmaP))).toFixed(3) * 1
    iCPU = (chartsValue.iUSL - iAVGx) / (3 * (chartsValue.iSigmaP))
    iCPL = (iAVGx - chartsValue.iLSL) / (3 * (chartsValue.iSigmaP))
    chartsValue.iCpk = (iCPU > iCPL ? iCPL : iCPU).toFixed(3) * 1

    let Cp, Ca
    Cp = (chartsValue.iUSL - chartsValue.iLSL) / (6 * (iAVGr / chartsValue.d_2))
    Ca = ((chartsValue.iUSL + chartsValue.iLSL) / 2 - iAVGx) / ((chartsValue.iUSL - chartsValue.iLSL) / 2)
    chartsValue.iPPM = 3 * Cp * (1 + Ca) + 3 * Cp * (1 - Ca)
  }
  if (iSagmax != 0) {
    chartsValue.iPp = ((chartsValue.iUSL - chartsValue.iLSL) / (6 * iSagmax)).toFixed(3) * 1
    iPPU = (chartsValue.iUSL - iAVGx) / (3 * iSagmax)
    iPPL = (iAVGx - chartsValue.iLSL) / (3 * iSagmax)
    chartsValue.iPpk = (iPPU > iPPL ? iPPL : iPPU).toFixed(3) * 1
  }
}